package com.jwgf.smarowindingmachine_unity.ui;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

import com.jwgf.common.ViewCache;
import com.jwgf.smarowindingmachine_unity.R;
import com.jwgf.smarowindingmachine_unity.logic.MachineParameterSharedPreference;

public class ParameterSettingMenuFragment extends MenuFragment implements
        OnClickListener {
    public static final String fragmentTag = "ParameterSettingMenuFragment";

    private Button systemBtn;
    private Button machineBtn;
    private Button inSegmentBtn;
    private Button groupBtn;
    private Button spindleBtn;
    private Button tubeProcessBtn;
    private Button spoolingDeviceBtn;
    private MachineParameterSharedPreference sp = MachineParameterSharedPreference.getInstance();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = ViewCache.getInstance().getView(R.layout.fragment_parameter_setting_menu);
//        View v = inflater.inflate(R.layout.fragment_parameter_setting_menu,
//                container, false);
        systemBtn = (Button) v.findViewById(R.id.btn_system);
        machineBtn = (Button) v.findViewById(R.id.btn_machine);
        inSegmentBtn = (Button) v.findViewById(R.id.btn_in_segment);
        groupBtn = (Button) v.findViewById(R.id.btn_group);
        spindleBtn = (Button) v.findViewById(R.id.btn_spindle);
        tubeProcessBtn = (Button) v.findViewById(R.id.btn_tube_process);
        spoolingDeviceBtn = (Button) v.findViewById(R.id.btn_spooling_device);
        systemBtn.setSelected(true);


        systemBtn.setOnClickListener(this);
        machineBtn.setOnClickListener(this);
        inSegmentBtn.setOnClickListener(this);
        groupBtn.setOnClickListener(this);
        spindleBtn.setOnClickListener(this);
        tubeProcessBtn.setOnClickListener(this);
        spoolingDeviceBtn.setOnClickListener(this);
        if (sp.getValue(MachineParameterSharedPreference.MACHINE_MACHINE_TYPE, "0").equals("0")) {
            tubeProcessBtn.setEnabled(false);
            tubeProcessBtn.setTextColor(getResources().getColor(R.color.disable));
            spoolingDeviceBtn.setEnabled(false);
            spoolingDeviceBtn.setTextColor(getResources().getColor(R.color.disable));
        } else if (sp.getValue(MachineParameterSharedPreference.MACHINE_MACHINE_TYPE, "0").equals("1")) {
            tubeProcessBtn.setEnabled(false);
            tubeProcessBtn.setTextColor(getResources().getColor(R.color.disable));
            spoolingDeviceBtn.setEnabled(true);
            spoolingDeviceBtn.setTextColor(getResources().getColorStateList(R.color.text_color_selector));
        } else if (sp.getValue(MachineParameterSharedPreference.MACHINE_MACHINE_TYPE, "0").equals("4")) {
            tubeProcessBtn.setEnabled(false);
            tubeProcessBtn.setTextColor(getResources().getColor(R.color.disable));
            spoolingDeviceBtn.setEnabled(true);
            spoolingDeviceBtn.setTextColor(getResources().getColorStateList(R.color.text_color_selector));
        } else {
            tubeProcessBtn.setEnabled(true);
            tubeProcessBtn.setTextColor(getResources().getColorStateList(R.color.text_color_selector));
            spoolingDeviceBtn.setEnabled(true);
            spoolingDeviceBtn.setTextColor(getResources().getColorStateList(R.color.text_color_selector));
        }
        return v;
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (sp.getValue(MachineParameterSharedPreference.MACHINE_MACHINE_TYPE, "0").equals("0")) {
            tubeProcessBtn.setEnabled(false);
            tubeProcessBtn.setTextColor(getResources().getColor(R.color.disable));
            spoolingDeviceBtn.setEnabled(false);
            spoolingDeviceBtn.setTextColor(getResources().getColor(R.color.disable));
        } else if (sp.getValue(MachineParameterSharedPreference.MACHINE_MACHINE_TYPE, "0").equals("1")) {
            tubeProcessBtn.setEnabled(false);
            tubeProcessBtn.setTextColor(getResources().getColor(R.color.disable));
            spoolingDeviceBtn.setEnabled(true);
            spoolingDeviceBtn.setTextColor(getResources().getColorStateList(R.color.text_color_selector));
        } else if (sp.getValue(MachineParameterSharedPreference.MACHINE_MACHINE_TYPE, "0").equals("4")) {
            tubeProcessBtn.setEnabled(false);
            tubeProcessBtn.setTextColor(getResources().getColor(R.color.disable));
            spoolingDeviceBtn.setEnabled(true);
            spoolingDeviceBtn.setTextColor(getResources().getColorStateList(R.color.text_color_selector));
        } else {
            tubeProcessBtn.setEnabled(true);
            tubeProcessBtn.setTextColor(getResources().getColorStateList(R.color.text_color_selector));
            spoolingDeviceBtn.setEnabled(true);
            spoolingDeviceBtn.setTextColor(getResources().getColorStateList(R.color.text_color_selector));
        }
    }

    @Override
    protected void switchSelection(int id) {
        if (id == R.id.btn_system) {
            systemBtn.setSelected(true);
            machineBtn.setSelected(false);
            inSegmentBtn.setSelected(false);
            groupBtn.setSelected(false);
            spindleBtn.setSelected(false);
            tubeProcessBtn.setSelected(false);
            spoolingDeviceBtn.setSelected(false);
        } else if (id == R.id.btn_machine) {
            systemBtn.setSelected(false);
            machineBtn.setSelected(true);
            inSegmentBtn.setSelected(false);
            groupBtn.setSelected(false);
            spindleBtn.setSelected(false);
            tubeProcessBtn.setSelected(false);
            spoolingDeviceBtn.setSelected(false);
        } else if (id == R.id.btn_in_segment) {
            systemBtn.setSelected(false);
            machineBtn.setSelected(false);
            inSegmentBtn.setSelected(true);
            groupBtn.setSelected(false);
            spindleBtn.setSelected(false);
            tubeProcessBtn.setSelected(false);
            spoolingDeviceBtn.setSelected(false);
        } else if (id == R.id.btn_group) {
            systemBtn.setSelected(false);
            machineBtn.setSelected(false);
            inSegmentBtn.setSelected(false);
            groupBtn.setSelected(true);
            spindleBtn.setSelected(false);
            tubeProcessBtn.setSelected(false);
            spoolingDeviceBtn.setSelected(false);
        } else if (id == R.id.btn_spindle) {
            systemBtn.setSelected(false);
            machineBtn.setSelected(false);
            inSegmentBtn.setSelected(false);
            groupBtn.setSelected(false);
            spindleBtn.setSelected(true);
            tubeProcessBtn.setSelected(false);
            spoolingDeviceBtn.setSelected(false);
        } else if (id == R.id.btn_tube_process) {
            systemBtn.setSelected(false);
            machineBtn.setSelected(false);
            inSegmentBtn.setSelected(false);
            groupBtn.setSelected(false);
            spindleBtn.setSelected(false);
            tubeProcessBtn.setSelected(true);
            spoolingDeviceBtn.setSelected(false);
        } else if (id == R.id.btn_spooling_device) {
            systemBtn.setSelected(false);
            machineBtn.setSelected(false);
            inSegmentBtn.setSelected(false);
            groupBtn.setSelected(false);
            spindleBtn.setSelected(false);
            tubeProcessBtn.setSelected(false);
            spoolingDeviceBtn.setSelected(true);
        }
    }

    @Override
    public void onClick(View v) {
        SwitchPageManager.getInstance().switchSecondMenu(getFragmentManager(), v.getId());
    }
}